package net.youpo.Market.UI;

import net.youpo.Market.MarketApplication;
import net.youpo.Market.R;
import net.youpo.Market.Download.DownloadManager;
import net.youpo.Market.Download.Downloads;
import net.youpo.Market.UI.HomePage.HomePageActivity;
import net.youpo.Market.UI.Manager.ManagerActivity;
import net.youpo.Market.UI.Ranking.RankingActivity;
import net.youpo.Market.UI.Search.SearchActivity;
import net.youpo.Market.UI.Type.TypeAcitvity;
import net.youpo.Market.Util.GlobalVariables;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class MainActivity extends TabActivity {
	private TabHost m_tabHost;
	private TextView titleTV;
	private LayoutInflater mLayoutInflater;
	private Class mBottomTabClassArray[] = { HomePageActivity.class,
			TypeAcitvity.class, RankingActivity.class, SearchActivity.class,
			ManagerActivity.class };
	private int mBottomIconArray[] = { R.drawable.main_tab_home,
			R.drawable.main_tab_category, R.drawable.main_tab_rank,
			R.drawable.main_tab_recommend, R.drawable.main_tab_app };
	private View[] mTabViewArr = new View[mBottomIconArray.length];// 选中的那个View
	private String mBottomTabTextArray[] = { "主页", "分类", "排行", "搜索", "管理" };
	private DownloadManager mDownloadManager;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
		init();
		mDownloadManager = new DownloadManager(this);
		if (getIntent().getBooleanExtra("need_update", false)) {
			final AlertDialog.Builder builder = new Builder(this);
			builder.setMessage("有新版本了，现在是否升级？");
			builder.setTitle("提示");
			builder.setPositiveButton("确认", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					mDownloadManager.insertDownload(getIntent()
							.getCharSequenceExtra("url").toString(),
							getString(R.string.app_name),
							Downloads.TYPE_UPDATE, MarketApplication
									.getMarketAppContext().getPackageName(), null, null, null);
				}
			});
			builder.setNegativeButton("取消", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			builder.create().show();
		}
	}

	private void init() {
		m_tabHost = getTabHost();
		mLayoutInflater = LayoutInflater.from(this);
		titleTV = (TextView) findViewById(R.id.titel_tv);
		titleTV.setText(getResources().getString(R.string.app_name));
		GlobalVariables.g_tabNumTag = (TextView) findViewById(R.id.tab_icon_num);

		int count = mBottomTabClassArray.length;
		for (int i = 0; i < count; i++) {
			TabSpec tabSpec = m_tabHost.newTabSpec(mBottomTabTextArray[i])
					.setIndicator(getTabItemView(i))
					.setContent(getTabItemIntent(i));
			m_tabHost.addTab(tabSpec);
			m_tabHost.setOnTabChangedListener(new OnTabChangeListener() {
				@Override
				public void onTabChanged(String tabId) {
					resetTabItem();
					int n = 0;
					if (tabId.equals(mBottomTabTextArray[0])) {// 点击首页
					} else if (tabId.equals(mBottomTabTextArray[1])) {// 点击类别
						n = 1;
					} else if (tabId.equals(mBottomTabTextArray[2])) {// 点击排行
						n = 2;
					} else if (tabId.equals(mBottomTabTextArray[3])) {// 点击搜索
						n = 3;
					} else if (tabId.equals(mBottomTabTextArray[4])) {// 点击管理
						n = 4;
					}

					View view = mTabViewArr[n];
					changeTabClick(n, view);
				}
			});
		}
		// 找到管理tab上的数字view
		GlobalVariables.g_tabNumTag = (TextView) mTabViewArr[mTabViewArr.length - 1]
				.findViewById(R.id.tab_icon_num);
		if (GlobalVariables.UPDATE_NUM != 0) {
			setTabNum(GlobalVariables.UPDATE_NUM + "", true);
		}
		//GlobalVariables.g_tabNumTag.setVisibility(View.VISIBLE);// =======================管理Tab上的数字提示
		resetTabItem();
	}

	
	  public static void setTabNum(String numString , boolean isVisible){
	    	if(isVisible) {
	    		GlobalVariables.g_tabNumTag.setVisibility(View.VISIBLE);
	    	} else {
	    		GlobalVariables.g_tabNumTag.setVisibility(View.GONE);
	    	}
	    	GlobalVariables.g_tabNumTag.setText(numString);
	    }
	
	
	/**
	 * Tab选中状态
	 * 
	 * @param index
	 * @param view
	 */
	private void changeTabClick(int index, View view) {
		LinearLayout tabLL = (LinearLayout) view.findViewById(R.id.tab_item_ll);
		tabLL.setBackgroundResource(R.drawable.main_tab_clicked);
		titleTV.setText(mBottomTabTextArray[index]);
	}

	/**
	 * 重置界面item
	 */
	private void resetTabItem() {
		for (int n = 0; n < mTabViewArr.length; n++) {
			View view = mTabViewArr[n];
			LinearLayout tabLL = (LinearLayout) view
					.findViewById(R.id.tab_item_ll);
			tabLL.setBackgroundResource(R.drawable.main_tab_bg);
			TextView textView = (TextView) view.findViewById(R.id.textview);
			textView.setTextColor(Color.WHITE);
		}
	}

	private View getTabItemView(int index) {
		View view = mLayoutInflater.inflate(R.layout.tab_item_view, null);
		ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
		if (imageView != null) {
			imageView.setImageResource(mBottomIconArray[index]);
			imageView.setVisibility(view.VISIBLE);
		}

		TextView textView = (TextView) view.findViewById(R.id.textview);
		textView.setText(mBottomTabTextArray[index]);
		textView.setTextColor(Color.WHITE);
		if (index == 0) {
			changeTabClick(index, view);
		}
		mTabViewArr[index] = view;
		return view;
	}

	private Intent getTabItemIntent(int index) {
		Intent intent = new Intent(this, mBottomTabClassArray[index]);
		return intent;
	}

	/**
	 * 创建菜单
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.mainactivity_menu, menu);
		return true;
	}

	/**
	 * 菜单点击事件
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int item_id = item.getItemId();
		switch (item_id) {
		case R.id.mainactivity_menu_setting:// 设置
			Intent intent = new Intent(MainActivity.this, SettingActivity.class);
			startActivity(intent);
			break;
		default:
			super.onOptionsItemSelected(item);
		}
		return true;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (m_tabHost.getCurrentTabTag().equals(mBottomTabTextArray[3])) {
			if (this.getCurrentActivity().onTouchEvent(event)) {
				return true;
			}
		}
		return false;
	}
}
